﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL.Object;

public partial class Controls_AdminGiaoVien : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            BindData();
        }
        
    }
    protected void lvGiaoVIen_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        
        
        if (e.CommandName == "Insert")
        {
            bool kq = Add(e.Item);
            
        }
        else
            if (e.CommandName == "Update")
            {

                bool kq = Update(e.Item);
                
            }
            //else
            //    if (e.CommandName == "Delete")
            //    {
            //       // lvGiaoVIen.SelectedIndex = e.NewSelectedIndex;

            //        //string id = lvGiaoVIen.SelectedDataKey.Value.ToString(); 

            //        bool kq = Delete());

            //    }
           
           
        //    TextBox txtFname = (TextBox)e.Item.FindControl("txtID");
        //    TextBox txtLname = (TextBox)e.Item.FindControl("txtMaGV");
        //    TextBox txtCtype = (TextBox)e.Item.FindControl("txtTenGV");

        //    bool kq = GiaoVien.Add();
        //    if (kq==true)
        //    {
        //        lblKQ.Text = "Thêm Thành Công";
        //    }
        //    else
        //    {
        //        lblKQ.Text = "Lỗi Xảy Ra";
        //    }

        BindData();
    }
    
    //Them
    protected bool Add(ListViewItem insertItem)
    {
        
            GiaoVien gv = new GiaoVien();

            gv.MaGV=((TextBox)insertItem.FindControl("txtMaGV")).Text;
            gv.TenGV=((TextBox)insertItem.FindControl("txtTenGV")).Text;
            gv.Email = ((TextBox)insertItem.FindControl("txtEmail")).Text;
       

        return GiaoVien.Add(gv);

    }
    protected void lvGiaoVIen_ItemInserted(object sender, ListViewInsertedEventArgs e)
    {
        BindData();
    }

    //Bind Da ta
    private void BindData()
    {
      
            lvGiaoVIen.DataSource = GiaoVien.All();
            lvGiaoVIen.DataBind();
        
    }
    protected void lvGiaoVIen_ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {

    }
    //update
    private bool Update(ListViewItem insertItem)
    {

        GiaoVien gv = new GiaoVien();
        gv.ID = int.Parse(((Label)insertItem.FindControl("lblID")).Text);
        gv.MaGV = ((TextBox)insertItem.FindControl("txtMaGV")).Text;
        gv.TenGV = ((TextBox)insertItem.FindControl("txtTenGV")).Text;
        gv.Email = ((TextBox)insertItem.FindControl("txtEmail")).Text;

        return GiaoVien.Add(gv);

    }

    //Delete
    private bool Delete(int idGV)
    {
        
       // string id=((Label)insertItem.FindControl("lblID")).Text;
        return GiaoVien.Delete(idGV);

    }

    protected void lvGiaoVIen_ItemDeleting(object sender, ListViewDeleteEventArgs e)
    {
        Label lbl = (Label)(lvGiaoVIen.Items[e.ItemIndex].FindControl("lblID"));

        if (lbl != null)
        {
            string id = lbl.Text;
            bool kq = GiaoVien.Delete(int.Parse(id));
        }
    }
}